module.exports = options => {
    const AdminUser = require('../model/adminUser')
    const jwt = require("jsonwebtoken")
    const { secret } = require("../config/key")
    return async (req, res, next) => {
        const token = String(req.headers.authorization || '').split(' ').pop()
        if (!token) {
            return res.status(401).send({
                message: '请提供token'
            })
        }
        const { id } = jwt.verify(token, secret)
        if (!id) {
            return res.status(401).send({
                message: '无效的token'
            })
        }
        req.user = await AdminUser.findById(id)
        if (!req.user) {
            return res.status(401).send({
                message: '请先登录'
            })
        }
        next()
    }
}